package bot.controller;

import bot.mapper.JokeMapper;
import bot.pojo.BotImage;
import bot.pojo.Joke;
import bot.pojo.JokeExample;
import bot.pojo.Res;
import com.alibaba.fastjson.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

@RestController
@RequestMapping("send/")
public class sendController {
    Random df = new Random();
    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    @GetMapping("getSend/{id}")
    public Object getsend(@PathVariable("id") Integer id){
        if(id == 1){
            String imageList = redisTemplate.opsForValue().get("imageList");
            if(imageList == null || imageList.equals("")){
                return null;
            }
            List<BotImage> nameList = JSONArray.parseArray(imageList, BotImage.class);
            int i = df.nextInt(nameList.size());
            return nameList.get(i);
        }else if (id == 2){
            String imageList = redisTemplate.opsForValue().get("HimageList");
            if(imageList == null || imageList.equals("")){
                return null;
            }
            List<BotImage> nameList = JSONArray.parseArray(imageList, BotImage.class);
            int i = df.nextInt(nameList.size());
            //直接返回对象，让bot转
            return nameList.get(i);
        }
        return null;
    }
//    JokeExample jokeExample = new JokeExample();
//    @Autowired
//    JokeMapper jokeMapper;
//    Joke joke = new Joke();
//    @GetMapping("/joker")
//    public void newjoke(Integer id){
//            String s = HttpHelp.sendGet("https://api.jisuapi.com/xiaohua/text?pagenum="+id+"&pagesize=20&sort=addtime&appkey=b92d9fc6a4bcc795");
//            Res re = JSONArray.parseObject(s, Res.class);
//            for (int i = 0; i < re.getResult().getList().size() ; i ++){
//                joke.setTextst(re.getResult().getList().get(i).getContent());
//                jokeMapper.insertSelective(joke);
//            }
//    }
//
//    @GetMapping("/getjoker")
//    public String getjoker(){
//        jokeExample.clear();
//        List<Joke> jokes = jokeMapper.selectByExample(jokeExample);
//        Random random = new Random();
//        int i = random.nextInt(jokes.size());
//        String textst = jokes.get(i).getTextst();
//        System.out.println(textst);
//        return textst;
//    }

}
